﻿/********************************************************************************************
 * 文件名称:	User.cs
 * 设计人员:	郑(matthewzjq@gmail.com)
 * 设计时间:	2009-03-16
 * 功能描述:	管理员
 * 注意事项:	
 * 版权所有:	Copyright (c) 2009, Fujian SIRC
 * 修改记录: 	修改时间		人员		修改备注
 *				    ----------		------		-------------------------------------------------
 *
 ********************************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using Shanfree.SharpMovie.Model;
using Shanfree.SharpMovie.IDAL;
using Shanfree.SharpMovie.DALFactory;
using Shanfree.Framework.Utility;
using System.Web.Security;

namespace Shanfree.SharpMovie.BLL
{
    /// <summary>
    /// 管理员
    /// </summary>
    public class User
    {
        private static readonly IUser dal = DataAccess.CreateUser();

        #region 加载用户信息
        /// <summary>
        /// 根据列表
        /// </summary>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <returns></returns>
        public UserInfo GetByID(string id)
        {
            if (string.IsNullOrEmpty(id) == true)
            {
                throw new ArgumentNullException("记录主键不能为空。");
            }
            return dal.GetByID(id);
        }

        /// <summary>
        /// 根据列表
        /// </summary>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <returns></returns>
        public IList<UserInfo> GetList(int pageSize, int pageIndex)
        {
            if (pageSize < 1 || pageIndex < 0)
            {
                throw new ArgumentNullException("pageSize or pageIndex ");
            }
            return dal.GetList(pageSize, pageIndex);
        }
        /// <summary>
        /// 获取好友列表
        /// </summary>
        /// <param name="ID">对象主键</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <returns>对象实体</returns>
        public IList<UserInfo> GetFriendList(string userID, int pageSize, int pageIndex)
        {
            if (string.IsNullOrEmpty(userID) == true)
            {
                throw new ArgumentNullException("记录主键不能为空。");
            }
            if (pageSize < 1 || pageIndex < 0)
            {
                throw new ArgumentNullException("pageSize or pageIndex ");
            }
            return dal.GetFriendList(userID, pageSize, pageIndex);
        }
        /// <summary>
        /// 获取用户的关注者列表
        /// </summary>
        /// <param name="ID">对象主键</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <returns>对象实体</returns>
        public IList<UserInfo> GetFollowerList(string userID, int pageSize, int pageIndex)
        {
            if (string.IsNullOrEmpty(userID) == true)
            {
                throw new ArgumentNullException("记录主键不能为空。");
            }
            if (pageSize < 1 || pageIndex < 0)
            {
                throw new ArgumentNullException("pageSize or pageIndex ");
            }
            return dal.GetFollowerList(userID, pageSize, pageIndex);
        }

        /// <summary>
        /// 获取评论
        /// </summary>
        /// <param name="name">name</param>
        /// <returns></returns>
        public UserInfo GetByName(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            return dal.GetByName(name);
        }

        #endregion

        #region 增删改
        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="name">人名</param>
        /// <param name="pwd">密码</param>
        /// <param name="rpwd">重复密码</param>
        /// <param name="email">电邮</param>
        /// <param name="remail">重复电邮</param>
        /// <returns>主键</returns>
        public string Add(string name, string pwd, string rpwd, string email, string remail)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("用户名称不能为空");
            }
            if (string.IsNullOrEmpty(pwd))
            {
                throw new ArgumentException("密码不能为空");
            }
            if (string.IsNullOrEmpty(email))
            {
                throw new ArgumentException("电子邮箱不能为空");
            }
            string err = string.Empty;
            if (pwd.Equals(rpwd) == false)
            {
                err = "您两次输入的密码不一致。";
            }
            if (email.Equals(remail) == false)
            {
                err += "您两次输入的电子邮箱不一致。";
            }
            if (string.IsNullOrEmpty(err) == false)
            {
                throw new ArgumentException(err);
            }

            UserInfo aInfo = new UserInfo();
            aInfo.Name = name;
            aInfo.Password = pwd;
            aInfo.Time = DateTime.Now;
            return dal.Add(aInfo);
        }
        /// <summary>
        /// 添加用户，管理员使用
        /// </summary>
        /// <param name="name">人名</param>
        /// <param name="pwd">密码</param>
        /// <param name="email">电邮</param>
        /// <returns>主键</returns>
        public string Add(string name, string pwd, string email)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("用户名称不能为空");
            }
            if (string.IsNullOrEmpty(pwd))
            {
                throw new ArgumentException("密码不能为空");
            }
            if (string.IsNullOrEmpty(email))
            {
                throw new ArgumentException("电子邮箱不能为空");
            }
            UserInfo aInfo = new UserInfo();
            aInfo.Name = name;
            aInfo.Password = pwd;
            aInfo.Time = DateTime.Now;
            return dal.Add(aInfo);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键</param>
        public void Delete(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentNullException("v");
            }
            UserInfo aInfo = new UserInfo();
            aInfo.ID = id;
            dal.Delete(aInfo);
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="name">人名</param>
        /// <param name="password">密码</param>
        /// <param name="email">电邮</param>
        public void Update(string name, string password, string email)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException("password");
            }
            if (string.IsNullOrEmpty(email))
            {
                throw new ArgumentNullException("email");
            }
            UserInfo aInfo = GetByName(name);
            password = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5");
            aInfo.Password = password;
            aInfo.Email = email;
            dal.Update(aInfo);
        }

        #endregion

        #region 登录
        /// <summary>
        /// 登录的方法
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="password">密码</param>
        /// <returns>是否登录</returns>
        public bool Login(string name, string password)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException("password");
            }
            //密码使用MD5加密
            password = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5");
            UserInfo aInfo = GetByName(name);
            if (null == aInfo || string.IsNullOrEmpty(aInfo.ID))
            {
                return false;
            }
            return password == aInfo.Password;
        }

        #endregion

        #region 用户个人信息
        /// <summary>
        /// 用户个人信息
        /// </summary>
        /// <param name="userID">用户主键</param>
        /// <returns>用户个人信息</returns>
        public UserInfo GetUserProfile(string userID)
        {
            if (string.IsNullOrEmpty(userID))
            {
                throw new ArgumentNullException("userID");
            }
            return dal.GetUserProfile(userID);
        }
        #endregion

    }
}
